package com.compunit.composite.core.cx;

import java.util.ArrayList;
import java.util.List;

import com.compunit.composite.core.interfaces.cx.ICxMapping;

/**
 * <code>CxMapping</code> defines the implementation
 * of the interface to class mapping data structure.
 *
 * @author Yi Wang (Neakor)
 * @version Creation date: 11-16-2009 16:31 EST
 * @version Modified date: 11-16-2009 16:34 EST
 */
public class CxMapping implements ICxMapping {
	/**
	 * The <code>Boolean</code> indicating if this mapping
	 * is a provided mapping. <code>true</code> if it is
	 * a provided mapping. <code>false</code> if it is a
	 * required mapping.
	 */
	private boolean provided;
	/**
	 * The <code>String</code> fully qualified
	 * interface name.
	 */
	private String interfacename;
	/**
	 * The <code>List</code> of <code>String</code>
	 * fully qualified class names.
	 */
	private final List<String> classnames;
	
	/**
	 * Constructor of <code>CxMapping</code>.
	 * @param provided The <code>Boolean</code>
	 * indicating if this mapping is a provided mapping.
	 * <code>true</code> if it is a provided mapping.
	 * <code>false</code> if it is a required mapping.
	 */
	public CxMapping(final boolean provided) {
		this.provided = provided;
		this.classnames = new ArrayList<String>();
	}

	@Override
	public void addClass(final String classname) {
		if(this.classnames.contains(classname)) return;
		this.classnames.add(classname);
	}

	@Override
	public void removeClass(final String classname) {
		this.classnames.remove(classname);
	}

	@Override
	public void setInterface(final String interfacename) {
		this.interfacename = interfacename;
	}

	@Override
	public void setProvided(final boolean provided) {
		this.provided = provided;
	}

	@Override
	public String getInterface() {
		return this.interfacename;
	}

	@Override
	public Iterable<String> getClasses() {
		return this.classnames;
	}
	
	@Override
	public int hashCode() {
		return this.interfacename.hashCode();
	}
	
	@Override
	public boolean equals(final Object o) {
		if(o instanceof CxMapping) {
			final CxMapping given = (CxMapping)o;
			if(given.interfacename == null) return this.interfacename==null;
			if(this.interfacename == null) return given.interfacename==null;
			return given.interfacename.equals(this.interfacename);
		}
		return false;
	}

	@Override
	public boolean isProvided() {
		return this.provided;
	}
}
